package com.arpa.ntocc.common.security.oauth;

import org.apache.oltu.oauth2.as.request.AbstractOAuthTokenRequest;
import org.apache.oltu.oauth2.as.validator.AuthorizationCodeValidator;
import org.apache.oltu.oauth2.as.validator.ClientCredentialValidator;
import org.apache.oltu.oauth2.as.validator.PasswordValidator;
import org.apache.oltu.oauth2.as.validator.RefreshTokenValidator;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.types.GrantType;
import org.apache.oltu.oauth2.common.utils.OAuthUtils;
import org.apache.oltu.oauth2.common.validators.OAuthValidator;

import javax.servlet.http.HttpServletRequest;

/**
 * oauth request
 * @author leo
 * Created by 秀荣 on 2015-01-08-0008.
 */
public class MyOAuthTokenRequest extends AbstractOAuthTokenRequest {
    public MyOAuthTokenRequest(HttpServletRequest request) throws OAuthSystemException, OAuthProblemException {
        super(request);
    }
    @Override
    protected OAuthValidator<HttpServletRequest> initValidator() throws OAuthProblemException, OAuthSystemException {
        final String clientIdValue = getClientId();
        if (OAuthUtils.isEmpty(clientIdValue)) {
            throw OAuthUtils.handleOAuthProblemException("客户端ID不能为空");
        }
        final String clientSecretValue = getClientSecret();
        if (OAuthUtils.isEmpty(clientSecretValue)) {
            throw OAuthUtils.handleOAuthProblemException("客户端安全码不能为空");
        }
        this.validators.put(GrantType.PASSWORD.toString(), PasswordValidator.class);
        this.validators.put(GrantType.CLIENT_CREDENTIALS.toString(), ClientCredentialValidator.class);
        this.validators.put(GrantType.AUTHORIZATION_CODE.toString(), AuthorizationCodeValidator.class);
        this.validators.put(GrantType.REFRESH_TOKEN.toString(), RefreshTokenValidator.class);
        return super.initValidator();
    }

    /**
     * 设备ID
     * @return
     */
    public String getDeviceId() {
        return this.getParam(MyOAuth.OAUTH_DEVICE_ID);
    }


    /**
     * 客户端ID
     * @return
     */
    @Override
    public String getClientId() {
        return this.getParam(MyOAuth.OAUTH_CLIENT_ID);
    }

    /**
     * 客户端安全码
     * @return
     */
    @Override
    public String getClientSecret() {
        return this.getParam(MyOAuth.OAUTH_CLIENT_SECRET);
    }
}
